package com.hyy.springbootblog.common.intercepter;

import com.hyy.springbootblog.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginIntercepter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //校验token是否正确

        //获取token(token默认在请求头中)
        String token = request.getHeader("user_header_token");
        log.info("header中获取的token:{}",token);

        //解析token
        Claims claims = JwtUtil.parseToken(token);
        if(claims==null){
            response.setStatus(401);
            return false;
        }

        //还可以校验map中信息是否正确，这里不做了
        return true;

    }
}
